package com.tjp.eight.views.emoj;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.tjp.eight.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class EmojiUtil {

    public EmojiUtil() {
        emojiAllList = new ArrayList<>();
        for (int i = 0; i < qqEmjArray.length; i++) {
            Emoji emoji = new Emoji();
            emoji.setImageUri(qqEmjArray[i]);
            emoji.setContent(QQEmojiTextArray[i]);
            emojiAllList.add(emoji);
        }
        for (int i = 0; i < wwEmjArray.length; i++) {
            Emoji emoji = new Emoji();
            emoji.setImageUri(wwEmjArray[i]);
            emoji.setContent(WWEmojiTextArray[i]);
            emojiAllList.add(emoji);
        }
    }

    private  ArrayList<Emoji> emojiList;


    private  ArrayList<Emoji> emojiAllList;
    private  ArrayList<Emoji> qqemojiList;
    private  ArrayList<Emoji> wwemojiList;

    public  ArrayList<Emoji> getEmojiList() {
        if (emojiList == null) {
            emojiList = generateEmojis();
        }
        return emojiList;
    }

    public  ArrayList<Emoji> getQQEmojiList() {
        if (qqemojiList == null) {
            qqemojiList = generateQQEmojis();
        }
        return qqemojiList;
    }

    public  ArrayList<Emoji> getWWEmojiList() {
        if (wwemojiList == null) {
            wwemojiList = generateWWEmojis();
        }
        return wwemojiList;
    }

    private static ArrayList<Emoji> generateEmojis() {
        ArrayList<Emoji> list = new ArrayList<>();
        for (int i = 0; i < EmojiResArray.length; i++) {
            Emoji emoji = new Emoji();
            emoji.setImageUri(EmojiResArray[i]);
            emoji.setContent(EmojiTextArray[i]);
            list.add(emoji);
        }
        return list;
    }

    private static ArrayList<Emoji> generateQQEmojis() {
        ArrayList<Emoji> list = new ArrayList<>();
        for (int i = 0; i < qqEmjArray.length; i++) {
            Emoji emoji = new Emoji();
            emoji.setImageUri(qqEmjArray[i]);
            emoji.setContent(QQEmojiTextArray[i]);
            list.add(emoji);
        }
        return list;
    }

    private static ArrayList<Emoji> generateWWEmojis() {
        ArrayList<Emoji> list = new ArrayList<>();
        for (int i = 0; i < wwEmjArray.length; i++) {
            Emoji emoji = new Emoji();
            emoji.setImageUri(wwEmjArray[i]);
            emoji.setContent(WWEmojiTextArray[i]);
            list.add(emoji);
        }
        return list;
    }

    public static final String[] WWEmojiTextArray = {
            "[ww0]", "[ww1]", "[ww2]", "[ww3]", "[ww4]", "[ww5]",
            "[ww6]", "[ww7]", "[ww8]", "[ww9]", "[ww10]", "[ww11]",
            "[ww12]", "[ww13]", "[ww14]", "[ww15]", "[ww16]", "[ww17]",
            "[ww18]", "[ww19]", "[ww20]", "[ww21]", "[ww22]", "[ww23]", "[ww24]",
            "[ww25]", "[ww26]", "[ww27]", "[ww28]", "[ww29]", "[ww30]", "[ww31]",
            "[ww32]", "[ww33]", "[ww34]", "[ww35]", "[ww36]", "[ww37]", "[ww38]",
            "[ww39]", "[ww40]",
            "[ww41]", "[ww42]", "[ww43]", "[ww44]", "[ww45]", "[ww46]", "[ww47]",
            "[ww48]", "[ww49]", "[ww50]", "[ww51]", "[ww52]", "[ww53]",
    };
    public static final int[] wwEmjArray = {
            R.drawable.ww_0,
            R.drawable.ww_1,
            R.drawable.ww_2,
            R.drawable.ww_3,
            R.drawable.ww_4,
            R.drawable.ww_5,
            R.drawable.ww_6,
            R.drawable.ww_7,
            R.drawable.ww_8,
            R.drawable.ww_9,
            R.drawable.ww_10,
            R.drawable.ww_11,
            R.drawable.ww_12,
            R.drawable.ww_13,
            R.drawable.ww_14,
            R.drawable.ww_15,
            R.drawable.ww_16,
            R.drawable.ww_17,
            R.drawable.ww_18,
            R.drawable.ww_19,
            R.drawable.ww_20,
            R.drawable.ww_21,
            R.drawable.ww_22,
            R.drawable.ww_23,
            R.drawable.ww_24,
            R.drawable.ww_25,
            R.drawable.ww_26,
            R.drawable.ww_27,
            R.drawable.ww_28,
            R.drawable.ww_29,
            R.drawable.ww_30,
            R.drawable.ww_31,
            R.drawable.ww_32,
            R.drawable.ww_33,
            R.drawable.ww_34,
            R.drawable.ww_35,
            R.drawable.ww_36,
            R.drawable.ww_37,
            R.drawable.ww_38,
            R.drawable.ww_39,
            R.drawable.ww_40,
            R.drawable.ww_41,
            R.drawable.ww_42,
            R.drawable.ww_43,
            R.drawable.ww_44,
            R.drawable.ww_45,
            R.drawable.ww_47,
            R.drawable.ww_48,
            R.drawable.ww_49,
            R.drawable.ww_50,
            R.drawable.ww_51,
            R.drawable.ww_52,
            R.drawable.ww_53,
    };
    public static final String[] QQEmojiTextArray = {
            "[0]",
            "[1]",
            "[2]",
            "[3]",
            "[4]",
            "[5]",
            "[6]", "[7]", "[8]", "[9]", "[10]", "[11]",
            "[12]", "[13]", "[14]", "[15]", "[16]", "[17]",
            "[18]", "[19]", "[20]", "[21]", "[22]", "[23]",
            "[24]", "[25]", "[26]", "[27]", "[28]", "[29]", "[30]",
            "[31]", "[32]", "[33]", "[34]", "[35]", "[36]", "[37]",
            "[38]", "[39]", "[40]", "[41]", "[42]", "[43]", "[44]",
            "[45]", "[46]", "[47]", "[48]", "[49]", "[50]", "[51]",
            "[52]", "[53]", "[54]", "[55]", "[56]", "[57]", "[58]", "[59]",
            "[60]", "[61]", "[62]", "[63]", "[64]", "[65]", "[66]", "[67]", "[68]",
            "[69]", "[70]", "[71]", "[72]", "[73]", "[74]", "[75]", "[76]", "[77]",
            "[78]", "[79]", "[80]", "[81]", "[82]", "[83]", "[84]", "[85]", "[86]", "[87]",
            "[88]", "[89]", "[90]", "[91]", "[92]", "[93]", "[94]", "[95]", "[96]", "[97]", "[98]",
            "[99]", "[100]", "[101]", "[102]", "[103]", "[104]",};
    public static final int[] qqEmjArray = {
            R.drawable.smiley_0,
            R.drawable.smiley_1,
            R.drawable.smiley_2,
            R.drawable.smiley_3,
            R.drawable.smiley_4,
            R.drawable.smiley_5,
            R.drawable.smiley_6,
            R.drawable.smiley_7,
            R.drawable.smiley_8,
            R.drawable.smiley_9,
            R.drawable.smiley_10,
            R.drawable.smiley_11,
            R.drawable.smiley_12,
            R.drawable.smiley_13,
            R.drawable.smiley_14,
            R.drawable.smiley_15,
            R.drawable.smiley_16,
            R.drawable.smiley_17,
            R.drawable.smiley_18,
            R.drawable.smiley_19,
            R.drawable.smiley_20,
            R.drawable.smiley_21,
            R.drawable.smiley_22,
            R.drawable.smiley_23,
            R.drawable.smiley_24,
            R.drawable.smiley_25,
            R.drawable.smiley_26,
            R.drawable.smiley_27,
            R.drawable.smiley_28,
            R.drawable.smiley_29,
            R.drawable.smiley_30,
            R.drawable.smiley_31,
            R.drawable.smiley_32,
            R.drawable.smiley_33,
            R.drawable.smiley_34,
            R.drawable.smiley_35,
            R.drawable.smiley_36,
            R.drawable.smiley_37,
            R.drawable.smiley_38,
            R.drawable.smiley_39,
            R.drawable.smiley_40,
            R.drawable.smiley_41,
            R.drawable.smiley_42,
            R.drawable.smiley_43,
            R.drawable.smiley_44,
            R.drawable.smiley_45,
            R.drawable.smiley_47,
            R.drawable.smiley_48,
            R.drawable.smiley_49,
            R.drawable.smiley_50,
            R.drawable.smiley_51,
            R.drawable.smiley_52,
            R.drawable.smiley_53,
            R.drawable.smiley_54,
            R.drawable.smiley_55,
            R.drawable.smiley_56,
            R.drawable.smiley_57,
            R.drawable.smiley_58,
            R.drawable.smiley_59,
            R.drawable.smiley_60,
            R.drawable.smiley_61,
            R.drawable.smiley_62,
            R.drawable.smiley_63,
            R.drawable.smiley_64,
            R.drawable.smiley_65,
            R.drawable.smiley_66,
            R.drawable.smiley_67,
            R.drawable.smiley_68,
            R.drawable.smiley_69,
            R.drawable.smiley_70,
            R.drawable.smiley_71,
            R.drawable.smiley_72,
            R.drawable.smiley_73,
            R.drawable.smiley_74,
            R.drawable.smiley_75,
            R.drawable.smiley_76,
            R.drawable.smiley_77,
            R.drawable.smiley_78,
            R.drawable.smiley_79,
            R.drawable.smiley_80,
            R.drawable.smiley_81,
            R.drawable.smiley_82,
            R.drawable.smiley_83,
            R.drawable.smiley_84,
            R.drawable.smiley_85,
            R.drawable.smiley_86,
            R.drawable.smiley_87,
            R.drawable.smiley_88,
            R.drawable.smiley_89,
            R.drawable.smiley_90,
            R.drawable.smiley_91,
            R.drawable.smiley_92,
            R.drawable.smiley_93,
            R.drawable.smiley_94,
            R.drawable.smiley_95,
            R.drawable.smiley_96,
            R.drawable.smiley_97,
            R.drawable.smiley_98,
            R.drawable.smiley_99,
            R.drawable.smiley_100,
            R.drawable.smiley_101,
            R.drawable.smiley_102,
            R.drawable.smiley_103,
            R.drawable.smiley_104,
    };

    public static final int[] EmojiResArray = {
            R.drawable.d_aini,
            R.drawable.d_aoteman,
            R.drawable.d_baibai,
            R.drawable.d_beishang,
            R.drawable.d_bishi,
            R.drawable.d_bizui,
            R.drawable.d_chanzui,
            R.drawable.d_chijing,
            R.drawable.d_dahaqi,
            R.drawable.d_dalian,
            R.drawable.d_ding,
            R.drawable.d_doge,
            R.drawable.d_feizao,
            R.drawable.d_ganmao,
            R.drawable.d_guzhang,
            R.drawable.d_haha,
            R.drawable.d_haixiu,
            R.drawable.d_han,
            R.drawable.d_hehe,
            R.drawable.d_heixian,
            R.drawable.d_heng,
            R.drawable.d_huaxin,
            R.drawable.d_jiyan,
            R.drawable.d_keai,
            R.drawable.d_kelian,
            R.drawable.d_ku,
            R.drawable.d_kun,
            R.drawable.d_landelini,
            R.drawable.d_lei,
            R.drawable.d_madaochenggong,
            R.drawable.d_miao,
            R.drawable.d_nanhaier,
            R.drawable.d_nu,
            R.drawable.d_numa,
            R.drawable.d_numa,
            R.drawable.d_qian,
            R.drawable.d_qinqin,
            R.drawable.d_shayan,
            R.drawable.d_shengbing,
            R.drawable.d_shenshou,
            R.drawable.d_shiwang,
            R.drawable.d_shuai,
            R.drawable.d_shuijiao,
            R.drawable.d_sikao,
            R.drawable.d_taikaixin,
            R.drawable.d_touxiao,
            R.drawable.d_tu,
            R.drawable.d_tuzi,
            R.drawable.d_wabishi,
            R.drawable.d_weiqu,
            R.drawable.d_xiaoku,
            R.drawable.d_xiongmao,
            R.drawable.d_xixi,
            R.drawable.d_xu,
            R.drawable.d_yinxian,
            R.drawable.d_yiwen,
            R.drawable.d_youhengheng,
            R.drawable.d_yun,
            R.drawable.d_zhajipijiu,
            R.drawable.d_zhuakuang,
            R.drawable.d_zhutou,
            R.drawable.d_zuiyou,
            R.drawable.d_zuohengheng,
            R.drawable.f_geili,
            R.drawable.f_hufen,
            R.drawable.f_jiong,
            R.drawable.f_meng,
            R.drawable.f_shenma,
            R.drawable.f_v5,
            R.drawable.f_xi,
            R.drawable.f_zhi,
            R.drawable.h_buyao,
            R.drawable.h_good,
            R.drawable.h_haha,
            R.drawable.h_lai,
            R.drawable.h_ok,
            R.drawable.h_quantou,
            R.drawable.h_ruo,
            R.drawable.h_woshou,
            R.drawable.h_ye,
            R.drawable.h_zan,
            R.drawable.h_zuoyi,
            R.drawable.l_shangxin,
            R.drawable.l_xin,
            R.drawable.o_dangao,
            R.drawable.o_feiji,
            R.drawable.o_ganbei,
            R.drawable.o_huatong,
            R.drawable.o_lazhu,
            R.drawable.o_liwu,
            R.drawable.o_lvsidai,
            R.drawable.o_weibo,
            R.drawable.o_weiguan,
            R.drawable.o_yinyue,
            R.drawable.o_zhaoxiangji,
            R.drawable.o_zhong,
            R.drawable.w_fuyun,
            R.drawable.w_shachenbao,
            R.drawable.w_taiyang,
            R.drawable.w_weifeng,
            R.drawable.w_xianhua,
            R.drawable.w_xiayu,
            R.drawable.w_yueliang,
    };

    public static final String[] EmojiTextArray = {
            "[爱你]",
            "[奥特曼]",
            "[拜拜]",
            "[悲伤]",
            "[鄙视]",
            "[闭嘴]",
            "[馋嘴]",
            "[吃惊]",
            "[哈欠]",
            "[打脸]",
            "[顶]",
            "[doge]",
            "[肥皂]",
            "[感冒]",
            "[鼓掌]",
            "[哈哈]",
            "[害羞]",
            "[汗]",
            "[微笑]",
            "[黑线]",
            "[哼]",
            "[色]",
            "[挤眼]",
            "[可爱]",
            "[可怜]",
            "[酷]",
            "[困]",
            "[白眼]",
            "[泪]",
            "[马到成功]",
            "[喵喵]",
            "[男孩儿]",
            "[怒]",
            "[怒骂]",
            "[女孩儿]",
            "[钱]",
            "[亲亲]",
            "[傻眼]",
            "[生病]",
            "[草泥马]",
            "[失望]",
            "[衰]",
            "[睡]",
            "[思考]",
            "[太开心]",
            "[偷笑]",
            "[吐]",
            "[兔子]",
            "[挖鼻]",
            "[委屈]",
            "[笑cry]",
            "[熊猫]",
            "[嘻嘻]",
            "[嘘]",
            "[阴险]",
            "[疑问]",
            "[右哼哼]",
            "[晕]",
            "[炸鸡啤酒]",
            "[抓狂]",
            "[猪头]",
            "[最右]",
            "[左哼哼]",
            "[给力]",
            "[互粉]",
            "[囧]",
            "[萌]",
            "[神马]",
            "[威武]",
            "[喜]",
            "[织]",
            "[NO]",
            "[good]",
            "[haha]",
            "[来]",
            "[OK]",
            "[拳头]",
            "[弱]",
            "[握手]",
            "[耶]",
            "[赞]",
            "[作揖]",
            "[伤心]",
            "[心]",
            "[蛋糕]",
            "[飞机]",
            "[干杯]",
            "[话筒]",
            "[蜡烛]",
            "[礼物]",
            "[绿丝带]",
            "[围脖]",
            "[围观]",
            "[音乐]",
            "[照相机]",
            "[钟]",
            "[浮云]",
            "[沙尘暴]",
            "[太阳]",
            "[微风]",
            "[鲜花]",
            "[下雨]",
            "[月亮]",
    };



    public static int calculateInSampleSize(BitmapFactory.Options options,
                                            int reqWidth, int reqHeight) {
        // 源图片的高度和宽度
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            // 计算出实际宽高和目标宽高的比率
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            // 选择宽和高中最小的比率作为inSampleSize的值，这样可以保证最终图片的宽和高
            // 一定都会大于等于目标的宽和高。
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                         int reqWidth, int reqHeight) {
        // 第一次解析将inJustDecodeBounds设置为true，来获取图片大小
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        // 调用上面定义的方法计算inSampleSize值
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        // 使用获取到的inSampleSize值再次解析图片
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }


    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public  void handlerEmojiText(TextView comment, String content, Context context) throws IOException {
        SpannableStringBuilder sb = new SpannableStringBuilder(content);
        String regex = "\\[(\\S+?)\\]";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(content);
        Iterator<Emoji> iterator;
        Emoji emoji = null;
        while (m.find()) {
            iterator = emojiAllList.iterator();
            String tempText = m.group();
            while (iterator.hasNext()) {
                emoji = iterator.next();
                if (tempText.equals(emoji.getContent())) {
                    //转换为Span并设置Span的大小
                    sb.setSpan(new ImageSpan(context, decodeSampledBitmapFromResource(context.getResources(), emoji.getImageUri()
                            , dip2px(context, 18), dip2px(context, 18))),
                            m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    break;
                }
            }
        }
        comment.setText(sb);
    }
    public  void handlerEmojiTextAppend(TextView comment, String content, Context context) throws IOException {
        SpannableStringBuilder sb = new SpannableStringBuilder(content);
        String regex = "\\[(\\S+?)\\]";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(content);
        Iterator<Emoji> iterator;
        Emoji emoji = null;
        while (m.find()) {
            iterator = emojiAllList.iterator();
            String tempText = m.group();
            while (iterator.hasNext()) {
                emoji = iterator.next();
                if (tempText.equals(emoji.getContent())) {
                    //转换为Span并设置Span的大小
                    sb.setSpan(new ImageSpan(context, decodeSampledBitmapFromResource(context.getResources(), emoji.getImageUri()
                            , dip2px(context, 18), dip2px(context, 18))),
                            m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    break;
                }
            }
        }
       // comment.setText(sb);
        comment.append(sb);
    }

    public  void handlerEmojiText(EditText comment, String content, Context context) throws IOException {
        SpannableStringBuilder sb = new SpannableStringBuilder(content);
        String regex = "\\[(\\S+?)\\]";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(content);
        Iterator<Emoji> iterator;
        Emoji emoji = null;
        while (m.find()) {
            iterator = emojiAllList.iterator();
            String tempText = m.group();
            while (iterator.hasNext()) {
                emoji = iterator.next();
                if (tempText.equals(emoji.getContent())) {
                    //转换为Span并设置Span的大小
                    sb.setSpan(new ImageSpan(context, decodeSampledBitmapFromResource(context.getResources(), emoji.getImageUri()
                            , dip2px(context, 18), dip2px(context, 18))),
                            m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    break;
                }
            }
        }
//        comment.setText(sb);
        int index = comment.getSelectionStart();
        Editable editable = comment.getEditableText();
        if (index < 0) {
            editable.append(sb);
        } else {
            editable.insert(index, sb);
        }
    }
}
